package com.itheima.mm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mm.mapper.GroupMapper;
import com.itheima.mm.mapper.PermissionsMapper;
import com.itheima.mm.pojo.permission.GroupPermission;
import com.itheima.mm.dto.Permissions;
import com.itheima.mm.pojo.permission.PermissionsAndGroup;
import com.itheima.mm.service.GroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
@Service
public class GroupServiceImpl extends ServiceImpl <GroupMapper, GroupPermission> implements GroupService {
    @Autowired
    private PermissionsMapper permissionSMapper;
    @Autowired
    private GroupMapper groupMapper;

    @Override
    public void updatepss(Integer id, Permissions permissions) {
        //更新名字
        GroupPermission groupPermission = new GroupPermission();
        groupPermission.setId(id);
        groupPermission.setName(permissions.getTitle());
        groupPermission.setUpdateTime(LocalDateTime.now());
        groupMapper.updateById(groupPermission);
        //更新权限组 先删后增原则
        LambdaQueryWrapper <PermissionsAndGroup> warpper = new LambdaQueryWrapper <>();
        warpper.eq(PermissionsAndGroup::getPgid, id);
        permissionSMapper.delete(warpper);

        Integer[] permissions1 = permissions.getPermissions();
        for (Integer integer : permissions1) {
            PermissionsAndGroup pg = new PermissionsAndGroup();
            pg.setPgid(id);
            pg.setPid(integer);
            permissionSMapper.insert(pg);
        }
    }

    @Override
    public String deleteById(Integer id) {
        //先删permissionGroup
        LambdaQueryWrapper <PermissionsAndGroup> warpper = new LambdaQueryWrapper <>();
        warpper.eq(PermissionsAndGroup::getPgid, id);
        permissionSMapper.delete(warpper);
        //后删group
        groupMapper.deleteById(id);
        return "删除成功";
    }

    @Override
    public String add(Permissions permissions) {
        GroupPermission groupPermission = new GroupPermission();
        groupPermission.setCreateTime(LocalDateTime.now());
        groupPermission.setUpdateTime(LocalDateTime.now());
        groupPermission.setName(permissions.getTitle());
        groupMapper.insert(groupPermission);
        Integer[] permissions1 = permissions.getPermissions();
        for (Integer integer : permissions1) {
            PermissionsAndGroup pag = new PermissionsAndGroup();
            pag.setPgid(permissions.getId());
            pag.setPid(integer);
            permissionSMapper.insert(pag);
        }

        return "添加成功";
    }

    @Override
    public Permissions selectById(Integer id) {
         Integer[] ints= permissionSMapper.seleteIntergerShuzu(id);
        GroupPermission groupPermission = groupMapper.selectById(id);
        Permissions permissions = new Permissions();
        permissions.setId(id);
        permissions.setPermissions(ints);
        permissions.setTitle(groupPermission.getName());
        permissions.setCreate_date(groupPermission.getCreateTime());
        return permissions;
    }
}
